package com.platform.qujia.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.platform.qujia.pojo.dto.ElectronicCardSubRes;
import com.platform.qujia.utils.MathUtil;
import lombok.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 客户电子会员卡（储蓄卡）
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class CustomerElectronicCard {
    public static final String cardOilsType_Gas = "1";
    public static final String cardOilsType_Diese = "2";
    public static final String cardOilsType_LNG = "3";
    public static final String cardOilsType_CNG = "4";

    private Integer id;     //主键
    private String unionId;     //微信用户唯一标识
    private String blogOpenid;      //公众号用户openId
    private String minaOpenid;      //小程序用户openId
    private String customerNo;      //会员编号
    private String customerName;    //客户名称
    private String mobilePhone;     //用户手机号
    /**
     * 实际充值金额
     */
    private BigDecimal amt;         //客户余额
    private String cardOilsType;        //电子会员卡类型：1，柴油；2.汽油
    /**
     * 实际存的是油企ID，多站通用时不为空
     */
    @Deprecated
    private Integer stationId;      //油站ID，单站使用时不为空
    private String stationName;     //油站
    private Date createTime;        //创建时间
    private Date recentlyTime;      //最近使用时间
    private String[] cardOilsTypeArray;     //参数数组

    /**
     * 存的是油站ID，单站使用时不为空
     */
    private Integer stationId2;

    /**
     * 赠送金额
     */
    private BigDecimal presentAmt;

    /**
     * 总金额，前端展示用
     */
    //private BigDecimal allAmt;

    /**
     * 油企id
     * @return
     */
    @JsonIgnore
    public Integer getEnterpriseId(){
        return this.stationId;
    }
    public BigDecimal getTotalAmt() {
        return MathUtil.add(this.amt, this.presentAmt);
    }

    public ElectronicCardSubRes subtractAmount(BigDecimal amount){
        ElectronicCardSubRes res = ElectronicCardSubRes.newInstance();
        if (amount == null) {
            res.setSuccess(false);
            return res;
        }
        if (amount.compareTo(BigDecimal.ZERO)==0) {
            res.setSuccess(true);
            return res;
        }
        if (this.getTotalAmt().compareTo(amount)<0) {
            res.setSuccess(false);
            return res;
        }
        if (this.amt != null) {
            BigDecimal newAmt = this.amt.subtract(amount);
            if (newAmt.compareTo(BigDecimal.ZERO)<0) {
                res.setAmt(this.amt);
                this.amt = BigDecimal.ZERO;
                this.presentAmt = this.presentAmt.add(newAmt);
                res.setPresentAmt(newAmt.abs());
            }else{
                res.setAmt(amount);
                this.amt = newAmt;
            }
        }else{
            this.presentAmt = this.presentAmt.subtract(amount);
            res.setPresentAmt(amount);
        }
        res.setSuccess(true);
        return res;
    }
}
